package com.hdcx.Minfresh.fragment;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.google.gson.Gson;
import com.hdcx.Minfresh.R;
import com.hdcx.Minfresh.adapter.MainAdapter;
import com.hdcx.Minfresh.bean.IndexWrapper;
import com.hdcx.Minfresh.upgrade.UpgradeUtils;
import com.hdcx.Minfresh.url.MyUrl;
import com.hdcx.Minfresh.util.AppUtil;
import com.hdcx.Minfresh.util.LogUtil;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;

import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by Administrator on 2016/11/9.
 */

public class MainFragment extends BaseFragment {
    private MainAdapter adapter;
    private RecyclerView recycler;

    @Override
    public int getLayout() {
        return R.layout.fragment_main;
    }

    @Override
    public void initView(View view) {
        recycler = (RecyclerView) view.findViewById(R.id.recycler);
    }

    @Override
    public void initData() {
        adapter = new MainAdapter(getActivity());
        recycler.setLayoutManager(new LinearLayoutManager(getActivity()));
        recycler.setAdapter(adapter);
        post_main();
        post_chek();
        AppUtil.setMain_Cart();
    }
/*
    type    *固定值　android
    version *本地获取的版本号*/
    private void post_chek() {

        UpgradeUtils.checkUpgrade(getActivity(), MyUrl.UPGRADE);

//        /*JSONObject json = new JSONObject();
//        try {
//            json.put("type", "android");
//            json.put("version", AppUtil.getAppVersion());
//
//            Log.i("version", "version: "+ AppUtil.getAppVersion() );
//
//        } catch (JSONException e) {
//            e.printStackTrace();
//        }
//        OkHttpUtils.postString()
//                .url(MyUrl.UPGRADE)
//                .content(json.toString())
//                .build()
//                .execute(new Callback<CheckWrapper>() {
//                    @Override
//                    public CheckWrapper parseNetworkResponse(Response response) throws IOException {
//                        String json = response.body().string();
//                        return new Gson().fromJson(json, CheckWrapper.class);
//
//                    }
//
//                    @Override
//                    public void onError(Call call, Exception e) {
//                        Log.i("onError", "onError: "+e);
//                        AppUtil.closeLoading();
//                    }
//
//                    @Override
//                    public void onResponse(final CheckWrapper response) {
//                        AppUtil.closeLoading();
//                        if (response.getStatus() == 1 && response.getIs_down().equals("1")) {
//                            if (response.getSrc() != null) {
//                                Uri uri = Uri.parse(response.getSrc());
//                                Intent it = new Intent(Intent.ACTION_VIEW, uri);
//                                startActivity(it);
//                            }
//                            AlertDialog  dialog =  new AlertDialog.Builder(mActivity).setTitle("更新提示")//设置对话框标题
//                                    .setMessage("有新的版本，请前往更新安装最新版本！")//设置显示的内容
//                                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {//添加确定按钮
//                                        @Override
//                                        public void onClick(DialogInterface dialog, int which) {//确定按钮的响应事件
//                                            mActivity.finish();
//                                        }
//                                    }).show();//在按键响应事件中显示此对话框
//                            dialog.setCanceledOnTouchOutside(false);
//                        }
//                    }
//                });*/
    }

    private void post_main() {
        AppUtil.showLoading(getActivity());
        JSONObject json = new JSONObject();
        OkHttpUtils.postString()
                .url(MyUrl.INDEX)
                .content(json.toString())
                .build()
                .execute(new Callback<IndexWrapper>() {
                    @Override
                    public IndexWrapper parseNetworkResponse(Response response) throws IOException {
                        String json = response.body().string();
                        LogUtil.LogShitou("首页数据",json);
                        return new Gson().fromJson(json, IndexWrapper.class);

                    }

                    @Override
                    public void onError(Call call, Exception e) {
                        Log.i("onError", "onError: "+e);
                        AppUtil.closeLoading();
                    }

                    @Override
                    public void onResponse(IndexWrapper response) {
                        AppUtil.closeLoading();
                        if (response.getStatus() == 1) {
                            adapter.setData(response);
                        }
                    }
                });
    }

    @Override
    public void onResume() {
        super.onResume();
        if(AppUtil.is_fresh){
            AppUtil.is_fresh= false;
            post_main();
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if(!hidden){
            if(AppUtil.is_fresh){
                AppUtil.is_fresh= false;
                post_main();
            }
        }
    }
}